﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NBA.DataAccess;
using NBA.Entity;
using System.Text;

namespace NBA.UI.web
{
    public partial class MatchList : System.Web.UI.Page
    {
        DaMatchInfo daMatchInfo = new DaMatchInfo();
        DaTeamInfo daTeamInfo = new DaTeamInfo();
        DaMatchTeamData daMatchTeamData = new DaMatchTeamData();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDate.Text = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
        }

        protected void btnGetList_Click(object sender, EventArgs e)
        {
            DateTime date = System.DateTime.Now;
            if (DateTime.TryParse(txtDate.Text, out date))
            {
                List<MatchInfo> matchInfoSet = daMatchInfo.GetMatchListByDate(date);
                StringBuilder output = new StringBuilder();

                foreach (MatchInfo matchInfo in matchInfoSet)
                {
                    List<MatchInfo> homeMatchInfoSet =
                        daMatchInfo.GetMatchListByTeamId(matchInfo.HomeTeamId, date.AddDays(1), 10);
                    List<MatchInfo> visitingMatchInfoSet =
                        daMatchInfo.GetMatchListByTeamId(matchInfo.VisitingTeamId, date.AddDays(1), 10);
                    List<MatchTeamData> homeMatchTeamDataSet =
                        daMatchTeamData.GetMatchTeamData(matchInfo.HomeTeamId,
                        homeMatchInfoSet.Select(s => s.MatchId).ToList());
                    List<MatchTeamData> visitingMatchDataSet =
                        daMatchTeamData.GetMatchTeamData(matchInfo.VisitingTeamId,
                        visitingMatchInfoSet.Select(s => s.MatchId).ToList());

                    output.Append(Output(homeMatchInfoSet, visitingMatchInfoSet, matchInfo.HomeTeamId, matchInfo.VisitingTeamId) + "<br />");
                }

                divOutput.InnerHtml = output.ToString();
            }
        }

        private string Output(List<MatchInfo> homeMatchInfoSet, List<MatchInfo> visitingMatchInfoSet,
            Guid homeTeamId, Guid visitingTeamId)
        {
            StringBuilder result = new StringBuilder();
            List<MatchTeamData> allAvg = daMatchTeamData.GetAllTeamAverage(2011);


            result.Append("<table><tr><td>");
            result.Append(Output(visitingMatchInfoSet, visitingTeamId, allAvg.Where(w => w.TeamId == visitingTeamId).FirstOrDefault()));
            result.Append("</td></tr><tr><td>");
            result.Append(Shot(visitingTeamId, visitingMatchInfoSet.Select(s => s.MatchId).ToList()));
            result.Append("</td></tr><tr><td>");
            result.Append(Output(homeMatchInfoSet, homeTeamId, allAvg.Where(w => w.TeamId == homeTeamId).FirstOrDefault()));
            result.Append("</td></tr><tr><td>");
            result.Append(Shot(homeTeamId, homeMatchInfoSet.Select(s => s.MatchId).ToList()));
            result.Append("</td></tr></table>");

            return result.ToString();
        }

        private string Output(List<MatchInfo> matchInfoSet, Guid teamId, MatchTeamData matchTeamData)
        {
            StringBuilder result = new StringBuilder();
            StringBuilder titleRow = new StringBuilder();
            StringBuilder firstRow = new StringBuilder();
            StringBuilder secondRow = new StringBuilder();

            //titleRow.Append("<td></td>");
            //firstRow.Append("<td>Home</td>");
            //secondRow.Append("<td>Visiting</td>");

            Dictionary<Guid, string> dicTeam = daTeamInfo.GetAllTeam();

            foreach (MatchInfo matchInfo in matchInfoSet)
            {
                titleRow.Append("<td>");
                titleRow.Append(matchInfo.MatchDateTime.ToString("yyyy-MM-dd"));
                titleRow.Append("</td>");

                string cssResult = "matchLose";
                if ((matchInfo.HomeTeamId == teamId && matchInfo.HomeScore > matchInfo.VisitingScore)
                    || (matchInfo.VisitingTeamId == teamId && matchInfo.HomeScore < matchInfo.VisitingScore))
                {
                    cssResult = "matchWin";
                }

                firstRow.Append("<td class='" + cssResult + "'>");
                firstRow.Append(dicTeam[matchInfo.VisitingTeamId] + "-" + dicTeam[matchInfo.HomeTeamId] +
                    "<br />" + matchInfo.VisitingScore + "-" + matchInfo.HomeScore);
                if (matchInfo.OtCount > 0)
                {
                    firstRow.Append("(" + matchInfo.OtCount + ")");
                }
                firstRow.Append("</td>");

                //secondRow.Append("<td class='" + visitingWin + "'>");
                //secondRow.Append(dicTeam[matchInfo.VisitingTeamId] + "<br />" + matchInfo.VisitingScore);
                //if (matchInfo.OtCount > 0)
                //{
                //    secondRow.Append("(" + matchInfo.OtCount + ")");
                //}
                //secondRow.Append("</td>");
            }
            result.Append("<table class='matchList'>");
            result.AppendFormat("<tr><td colspan='11'>{0}</td></tr>", matchTeamData.Scoring);
            result.Append("<tr>" + titleRow + "</tr>");
            result.Append("<tr>" + firstRow + "</tr>");
            //result.Append("<tr>" + secondRow + "</tr>");
            result.Append("</table>");

            return result.ToString();
        }

        protected string Shot(Guid teamId, List<Guid> matchIdSet)
        {
            StringBuilder result = new StringBuilder();

            result.Append("<table class='matchList'><tr>");
            List<MatchTeamData> matchTeamDataSet = daMatchTeamData.GetMatchTeamData(teamId, matchIdSet);
            foreach (MatchTeamData matchTeamData in matchTeamDataSet)
            {
                result.AppendFormat("<td>{0}({1},{2})</td>", matchTeamData.Shot2 + matchTeamData.Shot3,
                    matchTeamData.Shot2, matchTeamData.Shot3);
            }
            result.Append("</tr></table>");

            return result.ToString();
        }
    }
}